package com.dsiab.manageService.common;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    public ResponseMap handleAuthorizationException(HttpServletRequest req, HttpServletResponse res, Exception e) throws Exception{
        // 所有的异常都是进入该方法，并返回异常消息
        res.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return ResponseMap.error("用户信息失效，请重新登录");
    }

    @ExceptionHandler(Exception.class)
    public ResponseMap globalErrorHandler(HttpServletRequest req, HttpServletResponse res, Exception e) throws Exception{
        // 所有的异常都是进入该方法，并返回异常消息
        return ResponseMap.error(e.getCause().getMessage());
    }
}
